/*
 * @Description: 行按钮配置
 * @Author: HuiSir<https://gitee.com/huisir001>
 * @Date: 2025-08-04 19:17:37
 * @LastEditTime: 2025-09-16 17:36:52
 */
import { defineRowBtnsOptions } from "@/components/TablePage/predefined";
import useEditOptions from "./useEditOptions";
import { removeFileById, update } from "@/api/file/index";
import { IFormOption } from "@/ui";

export default defineRowBtnsOptions(
  (getGroupOptions: (formOptions: IFormOption[]) => Promise<void>) => ({
    editRequest: update,
    delRequest: removeFileById,
    editFormOptions: useEditOptions(),
    editDialogProps: { title: "编辑文件" },
    beforeShowEditDialog: async (row, formOptions) => {
      formOptions.value.forEach((option) => {
        if (option.key === "url") {
          option.desc = undefined;
          option.label = "文件地址";
          option.props!.showTextBtnIfFilesMoreThanLimit = false;
          option.props!.readonly = true;
          // 图片组件使用图片展示，方便预览
          if (row.value.width && row.value.height) {
            option.props!.listType = "picture-card";
            option.label = "图片预览";
          } else {
            option.props!.listType = "text";
            option.label = "文件地址";
          }
        }
        if (option.key === "group") {
          option.desc = undefined;
        }
        if (
          ["position", "width", "height", "size", "group"].includes(option.key)
        ) {
          option.props!.disabled = true;
        }
        if (option.key === "width") {
          option.hide = !row.value.width;
        }
        if (option.key === "height") {
          option.hide = !row.value.height;
        }
      });

      // 类目列表
      await getGroupOptions(formOptions.value);
    },
    beforeEditSubmit(formData) {
      const { id, name, remark } = formData.value;
      formData.value = {
        id,
        name,
        remark,
      };
    },
  })
);
